package cn.edu.stu.syllabus.library.list;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.adapters.BookAdapter;
import cn.edu.stu.syllabus.base.BaseActivity;
import cn.edu.stu.syllabus.base.BaseFragment;
import cn.edu.stu.syllabus.base.BaseQuickClickAdapter;
import cn.edu.stu.syllabus.base.StoreProxy;
import cn.edu.stu.syllabus.repository.library.local.BookEntry;
import cn.edu.stu.syllabus.library.detail.BookDetailActivity;
import cn.edu.stu.syllabus.utils.RecyclerPaddingUtils;
import cn.edu.stu.syllabus.utils.SwipeLayoutOffsetUtils;
import cn.edu.stu.syllabus.widgets.CustomLoadMoreView;
import cn.edu.stu.syllabus.widgets.CustomLayoutManger;

import static cn.edu.stu.syllabus.utils.NullPointerUtils.checkNotNull;

/**
 * Created by daidaijie on 2017/3/27.
 */

public class QueryBooksListFragment extends BaseFragment implements QueryBooksListContract.View,
        SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener,
        BaseActivity.OnToolbarDoubleClickListener {

    @BindView(R.id.libraryRecyclerView)
    RecyclerView mLibraryRecyclerView;

    private QueryBooksListContract.Presenter mPresenter;

    private BookAdapter mBookAdapter;

    private View mEmptyView;
    private View mErrorView;

    private View mHeadView;

    private CustomLayoutManger mCustomLayoutManger;

    public static QueryBooksListFragment newInstance() {
        QueryBooksListFragment fragment = new QueryBooksListFragment();
        return fragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_query_books_list;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

        mCustomLayoutManger = new CustomLayoutManger(mActivity);
        mCustomLayoutManger.setSpeedFast();
        mLibraryRecyclerView.setLayoutManager(mCustomLayoutManger);

        mBookAdapter = new BookAdapter(new ArrayList<BookEntry>());
        mBookAdapter.setOnLoadMoreListener(this);
        mBookAdapter.setLoadMoreView(new CustomLoadMoreView());
        mLibraryRecyclerView.setAdapter(mBookAdapter);
        mBookAdapter.setOnItemClickListener(new BaseQuickClickAdapter.OnItemClickListener<BookEntry>() {
            @Override
            public void onItemClick(BookEntry bookEntry, int position) {
                Intent intent = BookDetailActivity.newIntent(mActivity, bookEntry);
                QueryBooksListFragment.this.startActivity(intent);
            }
        });

        mSwipeRefreshLayout.setOnRefreshListener(this);

        SwipeLayoutOffsetUtils.downSwipeLayoutOffset(mActivity, mSwipeRefreshLayout);
        RecyclerPaddingUtils.fitRecyclerViewPadding(mActivity, mLibraryRecyclerView);

        mEmptyView = LayoutInflater.from(mActivity).inflate(R.layout.empty_view
                , (ViewGroup) mLibraryRecyclerView.getParent(), false);
        mErrorView = LayoutInflater.from(mActivity).inflate(R.layout.error_view
                , (ViewGroup) mLibraryRecyclerView.getParent(), false);

        TextView emptyTextView = (TextView) mEmptyView.findViewById(R.id.emptyTextView);
        emptyTextView.setText("查找不到搜索对应的结果");

        TextView errorTextView = (TextView) mErrorView.findViewById(R.id.errTextView);
        errorTextView.setText("加载失败，请点击重试");
        mErrorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefresh();
            }
        });

        mHeadView = LayoutInflater.from(mActivity).inflate(R.layout.head_query_book_num
                , (ViewGroup) mLibraryRecyclerView.getParent(), false);

        StoreProxy.onRestoreInstanceState(savedInstanceState, mPresenter);
        mPresenter.subscribe();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        StoreProxy.onSaveInstanceState(outState, mPresenter);
    }

    @Override
    public void setPresenter(QueryBooksListContract.Presenter presenter) {
        mPresenter = checkNotNull(presenter);
    }

    @Override
    public void showQueryNum(long num) {
        TextView textView = (TextView) mHeadView.findViewById(R.id.informationHeadTextView);
        textView.setText("共查找到" + num + "个结果");
        mBookAdapter.removeAllHeaderView();
        mBookAdapter.addHeaderView(mHeadView);
    }

    @Override
    public void setBookList(List<BookEntry> bookEntries) {
        mBookAdapter.setNewData(bookEntries);
    }

    @Override
    public void showRefreshing(boolean isShow) {
        mSwipeRefreshLayout.setRefreshing(isShow);
    }

    @Override
    public void appendBookList(List<BookEntry> bookEntries) {
        mBookAdapter.addData(bookEntries);
        mBookAdapter.loadMoreComplete();
    }

    @Override
    public void showLoadingFail() {
        mBookAdapter.loadMoreFail();
    }

    @Override
    public void showLoadingEnd() {
        mBookAdapter.loadMoreEnd();
    }

    @Override
    public void showDataEmpty() {
        mSwipeRefreshLayout.setEnabled(false);
        mBookAdapter.setEmptyView(mEmptyView);
    }

    @Override
    public void showDataError() {
        mBookAdapter.setEmptyView(mErrorView);
    }

    @Override
    public void moveToTop() {
        mCustomLayoutManger.scrollToPosition(0);
        showAppBar();
    }

    @Override
    public void setRecyclerViewEnable(boolean enable) {
        mCustomLayoutManger.setScrollEnabled(enable);
    }

    @Override
    public void onDestroyView() {
        mPresenter.unsubscribe();
        super.onDestroyView();
    }

    @Override
    public void onRefresh() {
        mPresenter.resetRefreshing();
        mPresenter.loadBookList();
    }

    @Override
    public void onLoadMoreRequested() {
        mPresenter.loadBookList();
    }

    @Override
    public void onToolbarDoubleClick() {
        mLibraryRecyclerView.smoothScrollToPosition(0);
    }
}
